package com.pure.common.examination;

import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 笔试
 * @date: 2022-12-27 11:25
 * @since 1.2.0
 **/
@Slf4j
public class WrittenExamination {

    public static void main(String[] args) {

//        // 1
//        String key = "jim.age";
//        Object value = 15;
//        set(new HashMap(), key, value);

//        // 2
//        String pattern = "My name is {0}, I am {1} years old";
//        Object[] obj = {"jim", 16};
//        System.out.println(format(pattern, obj));

        int[][] img = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {1, 259, 3, 4, 5}};
        prob(img);


//        // 4
//        String str = "hello,java,world,java,is,easy";
//        Set<String> strings = str2Set(str);
//        System.out.println(strings.toString());

//        // 5
//        Node root = Node.builder().value(1).next(
//                Node.builder().value(2).next(
//                        Node.builder().value(3).next(
//                                Node.builder().value(4).next(
//                                        Node.builder().value(5).next(null).build()
//                                ).build()
//                        ).build()
//                ).build()
//        ).build();
//        Node node = middleNode(root);
//        System.out.println(node.getValue());

    }

    public static void set(Map map, String key, Object value) {

        String[] split = key.split("\\.");
        String key1 = split[0];
        String key2 = split[1];

        Map children = new HashMap();
        children.put(key2, value);
        map.put(key1, children);

        System.out.println(map);
    }

    public static String format(String pattern, Object[] arr) {

        return MessageFormat.format(pattern, arr);
    }

    public static Double[] prob(int[][] img) {

        int count = 0;
        int prob = 0;
        for (int k = 0; k < img.length; k++) {
            for (int l = 0; l < img[k].length; l++) {
                count++;
                int i = img[k][l];
                if (i >= 0 && i <= 255) {
                    prob++;
                }
            }
        }
        System.out.println(count);
        System.out.println(prob);

        BigDecimal bigDecimalProb = BigDecimal.valueOf(prob);
        BigDecimal bigDecimalCount = BigDecimal.valueOf(count);
        BigDecimal divide = bigDecimalProb.divide(bigDecimalCount);
        double j = divide.doubleValue();
        System.out.println(divide.doubleValue());

        List<Double> list = Arrays.asList(j);
        Double[] d = list.toArray(new Double[0]);

        for (int i = 0; i < d.length; i++) {
            System.out.println("double[" + i + "] = " + d[i]);
        }
        return d;
    }


    public static Set<String> str2Set(String str) {

        String[] split = str.split(",");
        List<String> list = Arrays.stream(split).collect(Collectors.toList());
        Set<String> set = new LinkedHashSet<>();
        set.addAll(list);
        return set;
    }

    public static Node middleNode(Node root) {

        Node fast = root;
        Node slow = root;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    @Data
    @Builder
    static class Node {
        int value;
        Node next;
    }

}
